﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

using Sita.Entities;
using Sita.Data.Context;


namespace Sita.Data.Repositories
{
    public class BusinessUnitRepository : GenericRepository<BusinessUnit>
    {

        public BusinessUnitRepository() :
            base(new SitaContext())
        { }


        //public BusinessUnitRepository(IRoamFixContext context, IObjectServerAgent agent)
        //    : base(context)
        //{
        //    m_Agent = agent;
        //}

        //public BusinessUnitRepository(IRoamFixContext context)
        //    : base(context)
        //{
        //}

        //public BusinessUnit GetFullBusinessUnitByID(int buID)
        //{
        //    IRoamFixContext Context;
        //    BusinessUnit FullBU;

        //    Context = base.StoreContext as IRoamFixContext;

        //    FullBU = Context.BusinessUnits
        //    .Where(bu => bu.BusinessUnitID == buID)
        //    .Include("BusinessUnitPermissions")
        //    .Include("BusinessUnitReports")
        //    .Include("BusinessUnitResources")
        //    .Include("BusinessUnits1")
        //    .Include("BUsinessUnitUsers")
        //    .Include("BUsinessUnitUsers.User")
        //    .Include("BUsinessUnitUsers.User.UserPermissions")
        //    .SingleOrDefault();            
        //    return FullBU;



        //}


        public BusinessUnit GetBusinessUnitsByUserID(int userID)
        {
            SitaContext _context = new SitaContext();
            BusinessUnit UserBU = _context.Users.Include("BusinessUnit").Where(u => u.UserID == userID).FirstOrDefault().BusinessUnit;

            return UserBU;
        }

        public void UpdatePricesByBusinessUnitID(int businessUnitID)
        {
            SitaContext _context = new SitaContext();
            _context.UpdatePricesByBusinessUnitID(businessUnitID);
        }

        //public IList<BusinessUnit> GetParentAndChildsBusinessUnitsByUserID(int userID)
        //{
        //    IRoamFixContext Context;
        //    List<BusinessUnit> UserBUs;

        //    Context = base.StoreContext as IRoamFixContext;

        //    UserBUs = Context.BusinessUnits
        //        .ToList()
        //        .Join(Context.BusinessUnitUsers.Where(buu2 => buu2.UserID == userID), bu => bu.BusinessUnitID, buu => buu.BusinessUnitID, (bu, buu) => bu)
        //        .ToList();

        //    GetChildsBUsByParentID(ref UserBUs, UserBUs[0].BusinessUnitID);

        //    return UserBUs;
        //}

        //public void GetChildsBUsByParentID(ref List<BusinessUnit> BUs, int parentID)
        //{
        //    IRoamFixContext Context;
        //    List<BusinessUnit> UserBUs;

        //    Context = base.StoreContext as IRoamFixContext;

        //    UserBUs = Context.BusinessUnits.Where(bu => bu.ParentID == parentID).ToList();

        //    foreach (BusinessUnit currentBU in UserBUs)
        //    {
        //        BUs.Add(new BusinessUnit() { BusinessUnitID = currentBU.BusinessUnitID, Name = currentBU.Name });

        //        GetChildsBUsByParentID(ref BUs, currentBU.BusinessUnitID);
        //    }
        //}


        //public IList<BusinessUnitResource> GetBusinessUnitResources(int businessUnitID)
        //{
        //    IRoamFixContext Context;            

        //    Context = base.StoreContext as IRoamFixContext;

        //    return Context.BusinessUnitResources.Where(bu => bu.BusinessUnitID == businessUnitID).ToList();

        //}

        //public IList<string> GetOldBranches(bool onlyWithGRQContracts)
        //{
        //    return m_Agent.GetOldBranches(onlyWithGRQContracts).ToList();
        //}


        //public int RemoveChildrenResources(int buID)
        //{
        //    IRoamFixContext Context;

        //    Context = base.StoreContext as IRoamFixContext;

        //    return Context.RemoveChildrenResources(buID).FirstOrDefault().Value;
        //}


        //public void DeleteBusinessUnit(BusinessUnit bu)
        //{
        //    bu.Deleted = true;
        //    this.Modify(bu);
        //}
    }
}
